//
//  NumbAdController.m
//  DMGME
//
//  Created by ihope99 on 15/9/6.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "NumbAdController.h"
#import "ShopStateController.h"
@interface NumbAdController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation NumbAdController
{
    NSMutableArray *periodInfoArr;
    NSMutableArray *periodInfoArrId;
    UITableView *tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"期数选择";
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *labeAd = [[UILabel alloc] init];
    labeAd.frame = CGRectMake(15, 15, 150, 13);
    labeAd.font = [UIFont systemFontOfSize:13];
    labeAd.font = [UIFont boldSystemFontOfSize:15];
    labeAd.text = @"选择管理的期数";
    labeAd.textColor = ComColor;
    [self.view addSubview:labeAd];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor grayColor];
    lineView.frame = CGRectMake(0, 39, KWidth, 1);
    lineView.alpha = 0.5;
    [self.view addSubview:lineView];
    
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, KWidth, KHeight - 12) style:UITableViewStylePlain];
    tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    
    if ([_type_id isEqualToString:@"0"]) {
        [self bookNumList];
        
    }
    if ([_type_id isEqualToString:@"1"]){
        
        
        [self periodInfoNumList];
        
    }
    
    // Do any additional setup after loading the view.
   }

- (void)periodInfoNumList{
    
    periodInfoArr = [NSMutableArray array];
    periodInfoArrId = [NSMutableArray array];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$index"] forKey:@"token"];
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue: @"index" forKey:@"action"];
    [dict setValue: _magazine_id forKey:@"magazine_id"];
    [self StartLoading];
    [AFDownManager POST:BookManagePeriodNET parameters:dict success:^(id responseObject) {
//        NSLog(@"单行本期数 == %@",responseObject);
        [self StopLoading];
        if ([responseObject[@"data"] count] == 0) {
            return ;
        }
        for (int i = 0; i<=[responseObject[@"data"] count]-1; i++) {
            NSString *bookname;
            NSString *booknameID;
            bookname = [NSString stringWithFormat:@"%@",responseObject[@"data"][i][@"period"]];
            booknameID = [NSString stringWithFormat:@"%@",responseObject[@"data"][i][@"id"]];
            [periodInfoArr addObject:bookname];
            [periodInfoArrId addObject:booknameID];
            
        }

        [tableview reloadData];
//        NSLog(@"%@",responseObject[@"desc"]);
        
    } failure:^(NSError *error) {
//        [self StopLoading];
        [self showMsg:@"网络连接失败"];

    }];

    
}
- (void)bookNumList{

    periodInfoArr = [NSMutableArray array];
    periodInfoArrId = [NSMutableArray array];

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$index"] forKey:@"token"];
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue: @"index" forKey:@"action"];
    [dict setValue: _magazine_id forKey:@"magazine_id"];

    [self StartLoading];
        [AFDownManager POST:BookManagePeriodNET parameters:dict success:^(id responseObject) {
//            NSLog(@"期数选择 == %@",responseObject);
            [self StopLoading];
            if ([responseObject[@"data"] count] == 0) {
                return ;
            }
            for (int i = 0; i<=[responseObject[@"data"] count]-1; i++) {
                NSString *bookname;
                NSString *booknameID;
                bookname = [NSString stringWithFormat:@"%@",responseObject[@"data"][i][@"period"]];
                booknameID = [NSString stringWithFormat:@"%@",responseObject[@"data"][i][@"id"]];
                [periodInfoArr addObject:bookname];
                [periodInfoArrId addObject:booknameID];
                
            }

            [tableview reloadData];
//            NSLog(@"%@",responseObject[@"desc"]);
            
        } failure:^(NSError *error) {
//            [self StopLoading];
            [self showMsg:@"网络连接失败"];
        }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    ShopStateController *shouView = [[ShopStateController alloc] initWithNibName:@"ShopStateController" bundle:nil];
    shouView.numId = periodInfoArrId[indexPath.row];
    shouView.periodInfoId = periodInfoArrId[indexPath.row];
//    NSLog(@"=== %@",shouView.numId);
    [self.navigationController pushViewController:shouView animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return periodInfoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId =@"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@ 期",periodInfoArr[indexPath.row]];
    return cell;

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
